package permission

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"
	"private-domain-overseas-service/pkg/status/bizadmin"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/adminrouter"
	"private-domain-overseas-service/tools/ent/predicate"
	"time"
)

type AdminRouterDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAdminRouterDao(ctx context.Context, svcCtx *svc.ServiceContext) *AdminRouterDao {
	return &AdminRouterDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// AdminRouterCreate - 创建后台按钮
func (l *AdminRouterDao) AdminRouterCreate(createAdminRouterRequest *types.AdminRouterEditRequest, tx *ent.Tx) (int, error) {
	ormClient := l.svcCtx.Orm.AdminRouter
	if tx != nil {
		ormClient = tx.AdminRouter
	}

	adminRouter, err := ormClient.Create().
		SetAdminRouterMenuType(createAdminRouterRequest.AdminRouterMenuType).
		SetAdminRouterMenuName(createAdminRouterRequest.AdminRouterMenuName).
		SetAdminRouterIconType(createAdminRouterRequest.AdminRouterIconType).
		SetAdminRouterIcon(createAdminRouterRequest.AdminRouterIcon).
		SetAdminRouterRouteName(createAdminRouterRequest.AdminRouterRouteName).
		SetAdminRouterRoutePath(createAdminRouterRequest.AdminRouterRoutePath).
		SetAdminRouterComponent(createAdminRouterRequest.AdminRouterComponent).
		SetAdminRouterPathParam(createAdminRouterRequest.AdminRouterPathParam).
		SetAdminRouterStatus(createAdminRouterRequest.AdminRouterStatus).
		SetAdminRouterActiveMenu(createAdminRouterRequest.AdminRouterActiveMenu).
		SetAdminRouterHideInMenu(createAdminRouterRequest.AdminRouterHideInMenu).
		SetAdminRouterPid(createAdminRouterRequest.AdminRouterPid).
		SetAdminRouterSequence(createAdminRouterRequest.AdminRouterSequence).
		SetAdminRouterOrder(createAdminRouterRequest.AdminRouterOrder).
		SetAdminRouterI18nKey(createAdminRouterRequest.AdminRouterI18nKey).
		SetAdminRouterKeepAlive(createAdminRouterRequest.AdminRouterKeepAlive).
		SetAdminRouterConstant(createAdminRouterRequest.AdminRouterConstant).
		SetAdminRouterHref(createAdminRouterRequest.AdminRouterHref).
		SetAdminRouterMultiTab(createAdminRouterRequest.AdminRouterMultiTab).
		SetAdminRouterSystemType(createAdminRouterRequest.AdminRouterSystemType).
		Save(l.ctx)
	if err != nil {
		return 0, err
	}
	return adminRouter.ID, nil
}

// AdminRouterUpdate - 更新后台菜单
func (l *AdminRouterDao) AdminRouterUpdate(request *ent.AdminRouter, tx *ent.Tx) error {
	err := tx.AdminRouter.UpdateOneID(request.ID).
		SetAdminRouterMenuName(request.AdminRouterMenuName).
		SetAdminRouterIconType(request.AdminRouterIconType).
		SetAdminRouterIcon(request.AdminRouterIcon).
		SetAdminRouterRouteName(request.AdminRouterRouteName).
		SetAdminRouterComponent(request.AdminRouterComponent).
		SetAdminRouterPathParam(request.AdminRouterPathParam).
		SetAdminRouterStatus(request.AdminRouterStatus).
		SetAdminRouterActiveMenu(request.AdminRouterActiveMenu).
		SetAdminRouterHideInMenu(request.AdminRouterHideInMenu).
		SetAdminRouterPid(request.AdminRouterPid).
		SetAdminRouterSequence(request.AdminRouterSequence).
		SetAdminRouterOrder(request.AdminRouterOrder).
		SetAdminRouterI18nKey(request.AdminRouterI18nKey).
		SetAdminRouterKeepAlive(request.AdminRouterKeepAlive).
		SetAdminRouterConstant(request.AdminRouterConstant).
		SetAdminRouterHref(request.AdminRouterHref).
		SetAdminRouterMultiTab(request.AdminRouterMultiTab).
		SetAdminRouterRoutePath(request.AdminRouterRoutePath).
		SetAdminRouterSystemType(request.AdminRouterSystemType).
		SetUpdateAt(time.Now()).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// AdminRouterDelete - 删除后台菜单
func (l *AdminRouterDao) AdminRouterDelete(conditions []predicate.AdminRouter) error {
	err := l.svcCtx.Orm.AdminRouter.Update().
		Where(conditions...).
		SetIsDelete(bizadmin.Delete).
		SetDeleteAt(time.Now()).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// AdminRouterConditionsAll - 查询后台菜单列表
func (l *AdminRouterDao) AdminRouterConditionsAll(conditions []predicate.AdminRouter) ([]*ent.AdminRouter, int, error) {
	adminUsers, err := l.svcCtx.Orm.AdminRouter.Query().
		Where(conditions...).
		Where(adminrouter.IsDeleteEQ(bizadmin.NotDelete)).
		All(l.ctx)

	if err != nil {
		return nil, 0, err
	}

	total, err := l.svcCtx.Orm.AdminRouter.Query().
		Count(l.ctx)

	return adminUsers, total, nil
}

// AdminRouterFirst - 查询单个菜单
func (l *AdminRouterDao) AdminRouterFirst(conditions []predicate.AdminRouter) (*ent.AdminRouter, error) {
	adminRouter, err := l.svcCtx.Orm.AdminRouter.Query().
		Where(conditions...).
		Where(adminrouter.IsDeleteEQ(bizadmin.NotDelete)).
		First(l.ctx)

	if err != nil {
		return nil, err
	}

	return adminRouter, nil
}
